<template>
  <div class="page-search">
    <twt-form :form-items="formConfig" v-model="formData">
      <template #searchBtn>
        <slot name="searchBtn"></slot>
      </template>
    </twt-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, PropType } from 'vue'
import { IFormItem } from '@/base-ui/form/type'
import TwtForm from '@/base-ui/form'

export default defineComponent({
  props: {
    formConfig: {
      type: Array as PropType<IFormItem[]>,
      required: true
    }
  },
  components: {
    TwtForm
  },
  setup(props, { emit }) {
    const formItems = props.formConfig ?? []
    const formOriginData: any = {}
    for (const item of formItems) {
      formOriginData[item.field] = ''
    }
    const formData = ref(formOriginData)
    // 重置
    const handleRest = () => {
      for (const key in formOriginData) {
        formData.value[key] = formOriginData[key]
      }
    }
    // 查询
    const handleSearch = (): any => {
      return formData.value
    }
    return {
      formData,
      handleSearch,
      handleRest
    }
  }
})
</script>

<style scoped></style>
